package com.speechb.interceptor;



import com.alibaba.fastjson.JSONObject;
import com.speechb.modules.system.web.annotation.CheckParam;
import com.speechb.bean.ComParaBean;
import com.speechb.common.text.TextUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

public class RequestParamInteceptor extends HandlerInterceptorAdapter {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(handler instanceof HandlerMethod) {
            HandlerMethod methodHandler = (HandlerMethod) handler;
            CheckParam checkParam = methodHandler.getMethodAnnotation(CheckParam.class);
            //如果@AuthLogin (check=false) 这里的auth为false,即不用进行拦截验证，@AuthCheckAnnotation默认为前面定义的true　　
            if (checkParam != null ) {
                String [] values = checkParam.value();
                String para;
                for(String value:values){
                    if (checkParam.pattern()) {
                       // 需要正则校验
                       if (value.contains("=")) {
                           // 有等号，需要正则校验
                           String[] split = value.split("=");
                           para = request.getParameter(split[0]);
                           if(split.length>=2) {
                               if (!para.matches(split[1])) {
                                   // 校验失败
                                   respFailData(response,  "-4" ,"非法参数");
                                   return false;
                               }
                           }
                       } else {
                           para = request.getParameter(value);
                           if (StringUtils.isEmpty(para)) {
                               respFailData(response,  "-4" ,"参数丢失:"+value);
                               return false;
                           }
                       }
                    } else {
                        para = request.getParameter(value);
                        if(TextUtil.isEmpty(para)){
                            respFailData(response,  "-4" ,"参数丢失");
                            return false;
                        }
                    }
                }
            }
           boolean isNewSession= request.getSession().isNew();
            if(isNewSession){
               String compress= request.getHeader("compress");
               if(!TextUtil.isEmpty(compress)&&"false".equals(compress)){
                   ComParaBean.setIsCompress(false);
               }
            }
        }
        return true;
    }

    private void respFailData(HttpServletResponse response,String code, String message) throws IOException {
        response.setContentType("application/json;charset=utf-8");
        Writer writer = response.getWriter();
        Map map = new HashMap(2);
        map.put("code",code);
        map.put("message", message);
        String mapStr = JSONObject.toJSONString(map);
        System.out.println("登录拦截器访回Json为:" + mapStr);
        map.clear();
        writer.write(mapStr);
    }
}